package com.kye.express.business.waybill.waybill_detail.service;

import com.kye.base.Logger;
import com.kye.base.utils.DateUtils;
import com.kye.express.business.waybill.waybill_detail.SendArrivedDateModel;
import com.kye.express.business.waybill.waybill_detail.SendArrivedTimeModel;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Single;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

/**
 * @author : Joe
 * @version : 1.0
 * @editor : Joe
 * @created : 27/01/2018
 * @updated : 27/01/2018
 * @description : <Description>
 * @update_reason : <UpdateReason>
 *
 *     送达的日期数据业务, 每天的时间段每个3小时一个区间，选择天数保证7个24小时
 */

public class SendArrivedDateService {

    /**
     * 2018-01-10 05:27
     * 解析获取送达的日期数据
     *
     * @param date 当前运单显示的送达时间
     * @return
     */
    public Single<SendArrivedDateModel[]> getSendArrivedDateObservable(String date) {
        return
                Observable.just(date)
                        .map(new Function<String, Calendar>() {
                            @Override
                            public Calendar apply(String date) throws Exception {
                                Calendar planArrivedTime = DateUtils.dateStringToCalendar(date);
                                Logger.d("planArrivedTime : " + planArrivedTime);
                                return planArrivedTime;
                            }
                        })
                        /**
                         * 保证7个24小时的天数
                         */
                        .flatMap(new Function<Calendar, ObservableSource<List<Calendar>>>() {
                            @Override
                            public ObservableSource<List<Calendar>> apply(Calendar calendar) throws Exception {
                                List<Calendar> calendars = new ArrayList<>();
                                int hour = calendar.get(Calendar.HOUR_OF_DAY);
                                int min = calendar.get(Calendar.MINUTE);
                                int dayCount = 0;
                                if (hour == 0 && min == 0) {
                                    // 共7天
                                    dayCount = 7;
                                    for (int i = 0; i < dayCount; i++) {
                                        Calendar day = Calendar.getInstance();
                                        day.setTime(calendar.getTime());
                                        day.add(Calendar.DATE, i + 1);
                                        calendars.add(day);
                                    }
                                } else if (hour == 23 && min > 0) {
                                    // 共8天
                                    dayCount = 8;
                                    for (int i = 0; i < dayCount; i++) {
                                        Calendar day = Calendar.getInstance();
                                        day.setTime(calendar.getTime());
                                        day.add(Calendar.DATE, i + 1);
                                        calendars.add(day);
                                    }
                                } else {
                                    // 共8天
                                    dayCount = 8;
                                    for (int i = 0; i < dayCount; i++) {
                                        Calendar day = Calendar.getInstance();
                                        day.setTime(calendar.getTime());
                                        day.add(Calendar.DATE, i + 1);
                                        calendars.add(day);
                                    }
                                }

                                return Observable.just(calendars);
                            }
                        })
                        .flatMap(new Function<List<Calendar>, ObservableSource<Calendar>>() {
                            @Override
                            public ObservableSource<Calendar> apply(List<Calendar> calendars) throws Exception {
                                return Observable.fromIterable(calendars);
                            }
                        })
                        .map(new Function<Calendar, SendArrivedDateModel>() {
                            @Override
                            public SendArrivedDateModel apply(Calendar calendar) throws Exception {
                                SendArrivedDateModel sendArrivedDateModel = new SendArrivedDateModel();
                                String date = DateUtils.getDateStringByDateYearMonthDay(calendar.getTime());
                                sendArrivedDateModel.setDate(date);
                                // 显示格式：11月10日 (周一)
                                String messageShow = (calendar.get(Calendar.MONTH) + 1) + "月" + (calendar.get(Calendar.DAY_OF_MONTH)) + "日";
                                String dayWeekString = "";
                                switch (calendar.get(Calendar.DAY_OF_WEEK)) {
                                    case Calendar.MONDAY:
                                        dayWeekString = "(周一)";
                                        break;
                                    case Calendar.TUESDAY:
                                        dayWeekString = "(周二)";
                                        break;
                                    case Calendar.WEDNESDAY:
                                        dayWeekString = "(周三)";
                                        break;
                                    case Calendar.THURSDAY:
                                        dayWeekString = "(周四)";
                                        break;
                                    case Calendar.FRIDAY:
                                        dayWeekString = "(周五)";
                                        break;
                                    case Calendar.SATURDAY:
                                        dayWeekString = "(周六)";
                                        break;
                                    case Calendar.SUNDAY:
                                        dayWeekString = "(周日)";
                                        break;
                                }
                                sendArrivedDateModel.setMessageShow(messageShow + " " + dayWeekString);
                                return sendArrivedDateModel;
                            }
                        })
                        .toList()
                        .map(new Function<List<SendArrivedDateModel>, SendArrivedDateModel[]>() {
                            @Override
                            public SendArrivedDateModel[] apply(List<SendArrivedDateModel> sendArrivedDateModels) throws Exception {
                                SendArrivedDateModel[] days = new SendArrivedDateModel[sendArrivedDateModels.size()];
                                for (int i = 0; i < sendArrivedDateModels.size(); i++) {
                                    days[i] = sendArrivedDateModels.get(i);
                                }
                                return days;
                            }
                        });
    }

    /**
     * 获取一天所有时间区间, ArrayList<SendArrivedTimeModel>
     * 每个3小时一个时间段,显示格式: 00:00-03:00
     * @return
     */
    public Single<ArrayList<SendArrivedTimeModel>> getSendArrivedTimeNormalObservable() {
        return
                Single.just(new ArrayList<SendArrivedTimeModel>())
                        .doOnSuccess(new Consumer<List<SendArrivedTimeModel>>() {
                            @Override
                            public void accept(List<SendArrivedTimeModel> timeModels) throws Exception {
                                int startTime = 0;
                                int step = 3;
                                for (int hour = startTime; hour < 24; hour += step) {
                                    // 区间开始时间
                                    Calendar timeStart = Calendar.getInstance();
                                    timeStart.set(Calendar.HOUR_OF_DAY, hour);
                                    timeStart.set(Calendar.MINUTE, 0);
                                    timeStart.set(Calendar.SECOND, 0);
                                    // 区间结束时间
                                    Calendar timeEnd = Calendar.getInstance();
                                    timeEnd.setTime(timeStart.getTime());
                                    timeEnd.add(Calendar.HOUR_OF_DAY, 3);
                                    // HH:mm
                                    String dateStringByDateHourMinStart = DateUtils.getDateStringByDateHourMin(timeStart.getTime());
                                    String dateStringByDateHourMinEnd = DateUtils.getDateStringByDateHourMin(timeEnd.getTime());
                                    SendArrivedTimeModel timeModel = new SendArrivedTimeModel();
                                    timeModel.setStartTime(dateStringByDateHourMinStart + ":00");
                                    timeModel.setEndTime(dateStringByDateHourMinEnd + ":00");
                                    timeModel.setMessageShow(dateStringByDateHourMinStart + "-" + dateStringByDateHourMinEnd);
                                    timeModels.add(timeModel);
                                }
                            }
                        });
    }

    /**
     * 获取一天所有时间区间, SendArrivedTimeModel[]
     *
     * @return
     */
    public Single<SendArrivedTimeModel[]> getSendArrivedTimeNormalObservable2() {
        return
                Single.just(new ArrayList<SendArrivedTimeModel>())
                        .doOnSuccess(new Consumer<List<SendArrivedTimeModel>>() {
                            @Override
                            public void accept(List<SendArrivedTimeModel> timeModels) throws Exception {
                                int startTime = 0;
                                int step = 3;
                                for (int hour = startTime; hour < 24; hour += step) {
                                    // 区间开始时间
                                    Calendar timeStart = Calendar.getInstance();
                                    timeStart.set(Calendar.HOUR_OF_DAY, hour);
                                    timeStart.set(Calendar.MINUTE, 0);
                                    timeStart.set(Calendar.SECOND, 0);
                                    // 区间结束时间
                                    Calendar timeEnd = Calendar.getInstance();
                                    timeEnd.setTime(timeStart.getTime());
                                    timeEnd.add(Calendar.HOUR_OF_DAY, 3);
                                    // HH:mm
                                    String dateStringByDateHourMinStart = DateUtils.getDateStringByDateHourMin(timeStart.getTime());
                                    String dateStringByDateHourMinEnd = DateUtils.getDateStringByDateHourMin(timeEnd.getTime());
                                    SendArrivedTimeModel timeModel = new SendArrivedTimeModel();
                                    timeModel.setStartTime(dateStringByDateHourMinStart + ":00");
                                    timeModel.setEndTime(dateStringByDateHourMinEnd + ":00");
                                    timeModel.setMessageShow(dateStringByDateHourMinStart + "-" + dateStringByDateHourMinEnd);
                                    timeModels.add(timeModel);
                                }
                            }
                        })
                        .map(new Function<ArrayList<SendArrivedTimeModel>, SendArrivedTimeModel[]>() {
                            @Override
                            public SendArrivedTimeModel[] apply(ArrayList<SendArrivedTimeModel> sendArrivedTimeModels) throws Exception {
                                SendArrivedTimeModel[] normals = new SendArrivedTimeModel[sendArrivedTimeModels.size()];
                                for (int i = 0; i < sendArrivedTimeModels.size(); i++) {
                                    normals[i] = sendArrivedTimeModels.get(i);
                                }
                                return normals;
                            }
                        });
    }

    /**
     * 获取第一天时间区间
     * 第一天的时间区间从当前时间的往后一个区间开始
     * 如果当前时间为(HH:mm) 01:00, 当前可选从03:00-06:00开始
     * @param date
     * @return
     */
    public Single<SendArrivedTimeModel[]> getSendArrivedTimeFirstObservable(String date) {
        return
                getSendArrivedTimeNormalObservable()
                        .map(new Function<ArrayList<SendArrivedTimeModel>, ArrayList<SendArrivedTimeModel>>() {
                            @Override
                            public ArrayList<SendArrivedTimeModel> apply(ArrayList<SendArrivedTimeModel> sendArrivedTimeModels) throws Exception {
                                Calendar calendar = DateUtils.dateStringToCalendar(date, "yyyy-MM-dd HH:mm");
                                int startHour;
                                int hour = calendar.get(Calendar.HOUR_OF_DAY);
                                int min = calendar.get(Calendar.MINUTE);
                                int section = hour / 3;
                                int overflow = hour % 3;
                                if (section == 0 && overflow == 0) {
                                    return sendArrivedTimeModels;
                                }
                                if (section == 7 && overflow > 0) {
                                    return sendArrivedTimeModels;
                                }
                                if (section == 8 && overflow == 0) {
                                    return sendArrivedTimeModels;
                                }
                                if (overflow == 0) {
                                    startHour = section * 3;
                                } else {
                                    startHour = (section + 1) * 3;
                                }

                                sendArrivedTimeModels.clear();

                                for (int h = startHour; h < 24; h += 3) {

                                    // 区间开始时间
                                    Calendar timeStart = Calendar.getInstance();
                                    timeStart.set(Calendar.HOUR_OF_DAY, h);
                                    timeStart.set(Calendar.MINUTE, 0);
                                    timeStart.set(Calendar.SECOND, 0);

                                    // 区间结束时间
                                    Calendar timeEnd = Calendar.getInstance();
                                    timeEnd.setTime(timeStart.getTime());
                                    timeEnd.add(Calendar.HOUR_OF_DAY, 3);

                                    String dateStringByDateHourMinStart = DateUtils.getDateStringByDateHourMin(timeStart.getTime());
                                    String dateStringByDateHourMinEnd = DateUtils.getDateStringByDateHourMin(timeEnd.getTime());
                                    SendArrivedTimeModel timeModel = new SendArrivedTimeModel();
                                    timeModel.setStartTime(dateStringByDateHourMinStart + ":00");
                                    timeModel.setEndTime(dateStringByDateHourMinEnd + ":00");
                                    timeModel.setMessageShow(dateStringByDateHourMinStart + "-" + dateStringByDateHourMinEnd);

                                    sendArrivedTimeModels.add(timeModel);
                                }

                                return sendArrivedTimeModels;
                            }
                        })
                        .map(new Function<ArrayList<SendArrivedTimeModel>, SendArrivedTimeModel[]>() {
                            @Override
                            public SendArrivedTimeModel[] apply(ArrayList<SendArrivedTimeModel> sendArrivedTimeModels) throws Exception {
                                SendArrivedTimeModel[] firstDayTimes = new SendArrivedTimeModel[sendArrivedTimeModels.size()];
                                for (int i = 0; i < sendArrivedTimeModels.size(); i++) {
                                    firstDayTimes[i] = sendArrivedTimeModels.get(i);
                                }
                                return firstDayTimes;
                            }
                        });

    }

    /**
     * 获取最后一天的时间区间
     * 最后一天的可选区间为当前默认时间区间的之前的区间
     * @param date
     * @return
     */
    public Single<SendArrivedTimeModel[]> getSendArrivedTimeLastObservable(String date) {
        return
                getSendArrivedTimeNormalObservable()
                        .map(new Function<ArrayList<SendArrivedTimeModel>, ArrayList<SendArrivedTimeModel>>() {
                            @Override
                            public ArrayList<SendArrivedTimeModel> apply(ArrayList<SendArrivedTimeModel> sendArrivedTimeModels) throws Exception {
                                Calendar calendar = DateUtils.dateStringToCalendar(date, "yyyy-MM-dd HH:mm");
                                int endHour;
                                int hour = calendar.get(Calendar.HOUR_OF_DAY);
                                int min = calendar.get(Calendar.MINUTE);
                                int section = hour / 3;
                                int overflow = hour % 3;
                                if (section == 8 && overflow == 0) {
                                    return sendArrivedTimeModels;
                                }
                                if (section == 0) {
                                    endHour = 1 * 3;
                                } else {
                                    endHour = (section - 1) * 3;
                                }

                                sendArrivedTimeModels.clear();

                                for (int h = 0; h <= endHour; h += 3) {
                                    // 区间开始时间
                                    Calendar timeStart = Calendar.getInstance();
                                    timeStart.set(Calendar.HOUR_OF_DAY, h);
                                    timeStart.set(Calendar.MINUTE, 0);
                                    timeStart.set(Calendar.SECOND, 0);

                                    // 区间结束时间
                                    Calendar timeEnd = Calendar.getInstance();
                                    timeEnd.setTime(timeStart.getTime());
                                    timeEnd.add(Calendar.HOUR_OF_DAY, 3);

                                    String dateStringByDateHourMinStart = DateUtils.getDateStringByDateHourMin(timeStart.getTime());
                                    String dateStringByDateHourMinEnd = DateUtils.getDateStringByDateHourMin(timeEnd.getTime());
                                    SendArrivedTimeModel timeModel = new SendArrivedTimeModel();
                                    timeModel.setStartTime(dateStringByDateHourMinStart + ":00");
                                    timeModel.setEndTime(dateStringByDateHourMinEnd + ":00");
                                    timeModel.setMessageShow(dateStringByDateHourMinStart + "-" + dateStringByDateHourMinEnd);

                                    sendArrivedTimeModels.add(timeModel);
                                }
                                return sendArrivedTimeModels;
                            }
                        })
                        .map(new Function<ArrayList<SendArrivedTimeModel>, SendArrivedTimeModel[]>() {
                            @Override
                            public SendArrivedTimeModel[] apply(ArrayList<SendArrivedTimeModel> sendArrivedTimeModels) throws Exception {
                                SendArrivedTimeModel[] lastDayTimes = new SendArrivedTimeModel[sendArrivedTimeModels.size()];
                                for (int i = 0; i < sendArrivedTimeModels.size(); i++) {
                                    lastDayTimes[i] = sendArrivedTimeModels.get(i);
                                }
                                return lastDayTimes;
                            }
                        });
    }

}
